//
//  YBMeetingDetailViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBMeetingDetailViewController.h"
#import "YBMessageImageModel.h"
#import "ZJWebViewController.h"
#import "YBSignInPeopleInfoListViewController.h"

@interface YBMeetingDetailViewController ()

/**  back  */
@property (strong, nonatomic) UIScrollView  *backScrollView;

@property (nonatomic , strong) UIButton * scanButton;

@property (nonatomic , strong) NSArray<YBMessageImageModel *> * imageArray;
@end

@implementation YBMeetingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titlelabel.text = self.model.TITLE;
//    [self ybInitUserInterface];
    [self ybInitDataSource];
}


-(void)ybInitUserInterface{
    [self.view addSubview:self.backScrollView];
    
    UILabel * currentLabel = nil;
    NSArray * titleArray = @[@"" ,@"会议内容:" , @"会议地点:" , @"会议时间:" , @"联 系 人:" , @"联系电话:" , @"发布单位:" , @"发布日期:" , @"截止日期:" , @"上传照片(不得上传任何涉密照片):"];
    NSArray * dataArray = @[[ToolClass returnNoNullStringWithString:self.model.TITLE] , [ToolClass returnNoNullStringWithString:self.model.CONTENT] , [ToolClass returnNoNullStringWithString:self.model.ADDRESS] , [ToolClass returnNoNullStringWithString:self.model.HY_START_TIME], [ToolClass returnNoNullStringWithString:self.model.CONTACT_NAME] , [ToolClass returnNoNullStringWithString:self.model.CONTACT_MOBILE] , [ToolClass returnNoNullStringWithString:self.model.PUBLISH_UNIT] , [ToolClass returnNoNullStringWithString:self.model.START_TIME] , [ToolClass returnNoNullStringWithString:self.model.END_TIME] , @""];
    
    for (int i = 0 ; i < titleArray.count; i ++) {
        if (i == titleArray.count - 1) {
            if (self.imageArray.count == 0) {
                break;
            }
        }
        NSString * theString = [NSString stringWithFormat:@"%@%@" , titleArray[i] , dataArray[i]];
        UILabel * label = [ToolClass creatLabelWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 60) font:15 textColor:[UIColor textBlackColor] text:theString];
        CGFloat theHeight = [theString boundingRectWithSize:CGSizeMake([UIScreen zj_screenWith] - ItemWidth * 60, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(15)} context:nil].size.height;
        [self.backScrollView addSubview:label];
        if (i == 0) {
            label.sd_layout.leftSpaceToView(self.backScrollView , ItemWidth * 30).topSpaceToView(self.backScrollView , ItemHeight * 15).widthIs([UIScreen zj_screenWith] - ItemWidth * 60 + 1).heightIs(theHeight + 1);
        }
        else{
            label.sd_layout.leftSpaceToView(self.backScrollView , ItemWidth * 30).topSpaceToView(currentLabel , ItemHeight * 30).widthIs([UIScreen zj_screenWith] - ItemWidth * 60 + 1).heightIs(theHeight + 1);
        }
        currentLabel = label;
    }
    
    UIImageView * currentImageView = nil;
    
    for (int j = 0; j < self.imageArray.count; j ++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith] - ItemWidth * 400, ItemHeight * 400)];
        [imageView sd_setImageWithURL:CREATEURL(self.imageArray[j].RES_URL) placeholderImage:PlaceholderImage];
        [self.backScrollView addSubview:imageView];
        if (j == 0) {
            imageView.sd_layout.leftSpaceToView(self.backScrollView , [UIScreen zj_screenWith] * 0.25).topSpaceToView(currentLabel , ItemHeight * 30).widthIs([UIScreen zj_screenWith] * 0.5).heightIs([UIScreen zj_screenHeight] * 0.5);
        }
        else{
            imageView.sd_layout.leftSpaceToView(self.backScrollView , [UIScreen zj_screenWith] * 0.25).topSpaceToView(currentImageView , ItemHeight * 30).widthIs([UIScreen zj_screenWith] * 0.5).heightIs([UIScreen zj_screenHeight] * 0.5);
        }
        currentImageView = imageView;
    }
    if (currentImageView) {
        [self.backScrollView setupAutoContentSizeWithBottomView:currentImageView bottomMargin:ItemHeight * 50];
    }
    else{
        [self.backScrollView setupAutoContentSizeWithBottomView:currentLabel bottomMargin:ItemHeight * 50];
    }
    
    if (self.isShowButton) {
        [self.view addSubview:self.scanButton];
    }
    
    
    
}


-(void)ybInitDataSource{
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    [YBRequestManager request_releaseMeetingDetailWithHyId:self.model.ID successBlock:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            self.model = [YBMeetingReleaseModel yy_modelWithJSON:object[@"HY"][0]];
            self.imageArray = [NSArray yy_modelArrayWithClass:[YBMessageImageModel class] json:object[@"MSG_RES"]];
            [self ybInitUserInterface];
        }
        else{
            [YJProgressHUD showSuccess:@"加载失败" inview:self.view];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}


-(void)option_scanButton:(UIButton *)sender{
    YBSignInPeopleInfoListViewController * vc = [[YBSignInPeopleInfoListViewController alloc]init];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)option_signInButton:(UIButton *)sender{
    ZJWebViewController *vc = [ZJWebViewController new];
    NSString * unitId = @"";
    NSString * unitName = @"";
    if (ZJUSERINFORMODEL.DEPT_MODEL.count > 0) {
        unitId = [ToolClass returnNoNullStringWithString:ZJUSERINFORMODEL.DEPT_MODEL[0].ID];
        unitName = [ToolClass returnNoNullStringWithString:ZJUSERINFORMODEL.DEPT_MODEL[0].F_DEPT_NAME];
    }
    NSString *urlString = [NSString stringWithFormat:@"%@/miit/view/writeHZD.html?ACTID=%@&ACCOUNT=%@&UNITNAME=%@",HTMLURL,self.model.ID , ZJUSERINFORMODEL.ORG_ACCOUNT.ID , unitName];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    vc.titleString = @"报名";
    vc.urlString = urlString;
    [self.navigationController pushViewController:vc animated:YES];
}


- (UIScrollView *)backScrollView{
    if (!_backScrollView ) {
        _backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0 , navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - navHeight - ItemHeight * 100)];
        if (!self.isShowButton) {
            _backScrollView.frame = CGRectMake(0 , navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - navHeight);
        }
        _backScrollView.showsVerticalScrollIndicator = NO;
        _backScrollView.showsHorizontalScrollIndicator = NO;
        _backScrollView.contentSize = _backScrollView.size;
        _backScrollView.backgroundColor = [UIColor whiteColor];
    }
    return _backScrollView;
    
}


-(UIButton *)scanButton
{
    if (!_scanButton) {
        _scanButton = [ToolClass createButtonWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - ItemHeight * 100, [UIScreen zj_screenWith], ItemHeight * 100) text:@"" textColor:[UIColor whiteColor] imageName:@"" font:18];
        _scanButton.backgroundColor = [UIColor themeColor];
        if (self.isSignIn) {
            [_scanButton setTitle:@"报名" forState:UIControlStateNormal];
            [_scanButton addTarget:self action:@selector(option_signInButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        else{
            [_scanButton setTitle:@"查看报名信息" forState:UIControlStateNormal];
            [_scanButton addTarget:self action:@selector(option_scanButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    return _scanButton;
}

@end
